﻿using Xejen.Database.Entities;
using Xejen.Database.Entities.Auditing;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;

namespace Xejen.Database.Ef.Sqlite.Demos
{
    /// <summary>
    /// 表示协议请求记录表[ProtocolRecords]
    /// </summary>
    /// <creator>marc</creator>
    [Table("ProtocolRecords")]
    [Description("请求记录表")]
    public class ProtocolRecordEntity : CreationEntity<long>, IEntity<long>
    {
        /// <inheritdoc/>
        [Key]
        [Column(Order = 100)]
        [Description("主键id")]
        public override long Id { get; set; }

        /// <summary>
        /// 线程Id，若为空，则说明是同步。若为异步请求，则必有线程Id
        /// </summary>
        [Column(Order = 200)]
        [Description("线程Id")]
        public virtual int? TaskId { get; set; }

        /// <summary>
        /// 请求地址
        /// </summary>
        [Required]
        [Column(Order = 250)]
        [Description("请求地址")]
        public virtual string Address { get; set; }

        /// <summary>
        /// 发送的数据
        /// </summary>
        [Column(Order = 300)]
        [Description("发送的数据")]
        public string SendData { get; set; }

        /// <summary>
        /// 响应数据
        /// </summary>
        [Column(Order = 400)]
        [Description("响应数据")]
        public string ResponseData { get; set; }

        /// <summary>
        /// 是否成功
        /// </summary>
        [Column(Order = 500)]
        [Description("是否成功")]
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        [Column(Order = 600)]
        [Description("错误信息")]
        public string ErrorMessage { get; set; }

        /// <summary>
        /// 请求耗时的总毫秒数
        /// </summary>
        [Column(Order = 700)]
        [Description("请求耗时的总毫秒数")]
        public double DurationTotalMilliseconds { get; set; }

        /// <summary>
        /// 请求耗时
        /// </summary>
        [NotMapped]
        public TimeSpan Duration
        {
            get { return TimeSpan.FromMilliseconds(DurationTotalMilliseconds); }
            set { DurationTotalMilliseconds = value.TotalMilliseconds; }
        }

        /// <summary>
        /// 请求起时时间
        /// </summary>
        [Column(Order = 800)]
        [Description("请求起时时间")]
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 请求结束时间
        /// </summary>
        [Column(Order = 900)]
        [Description("请求结束时间")]
        public DateTime EndTime { get; set; }

        /// <inheritdoc cref="ProtocolRecordEntity"/>
        public ProtocolRecordEntity()
        {
            CreationTime = StandardTime.Now;
        }
    }
}
